package org.jeecg.modules.haili.utils;

import org.jeecg.modules.haili.config.InsuranceConfigProperties;
import org.jeecg.modules.haili.entity.fieldname.*;
import org.jeecg.modules.haili.entity.insurance.OriginInsOrder;
import org.jeecg.modules.haili.entity.request.*;
import org.jeecg.modules.haili.entity.response.MessageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class MessageBuilder {

    @Autowired
    InsuranceConfigProperties insuranceConfigProperties;

    /**
     * 将投保信息转换成JSON报文
     */
    public Message convertOrignOrderToJsonMessage(OriginInsOrder originInsOrder) {
        SimpleDateFormat rs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        RequestHead requestHead = RequestHead.builder()
                .BUSINESS_UUID(originInsOrder.getUuid())
                .CHANNEL_CODE(insuranceConfigProperties.getCHANNEL_CODE())
                .CLIENT_ENCODE(insuranceConfigProperties.getCLIENT_ENCODE())
                .INTERFACE_CODE(insuranceConfigProperties.getUNDER_WRITINT_INTERFACE_CODE_1())
                .INTERFACE_PWD(insuranceConfigProperties.getINTERFACE_PWD())
                .INTERFACE_USER_CODE(insuranceConfigProperties.getINTERFACE_USER_CODE())
                .REQUEST_TIME(rs.format(new Date()))
                .SERVE_CODE(insuranceConfigProperties.getSERVE_CODE())

                .XML_LIST_SUFFIX("").build();
        //构造投保基本信息

        BaseRelFieldName baseRelFieldName = BaseRelFieldName.builder()
//                .call_back_url("")
//                .extenterp_code("")
                .plcy_bgn_time(InsUtils.formatYdate(originInsOrder.getTt()))
                .plcy_end_time(InsUtils.oneMonthLater(originInsOrder.getTt()))
                .third_order_no(originInsOrder.getId() + "")
                .redirect_url(insuranceConfigProperties.getRedirect_url())
                .rate(InsUtils.dotRate(originInsOrder.getShlb())).build();
        CommonLable base = CommonLable.builder()
                .prodCode(insuranceConfigProperties.getProd_code())
                .relTbl("ins_appl_main")
                .relFieldName(baseRelFieldName).build();
        //构造投保人信息
        ApplicantFieldName applicantFieldName = ApplicantFieldName.builder()
                .appl_name(originInsOrder.getName())
                .certf_no(originInsOrder.getIdcard())
                .certf_type("01")
                .clnt_type("1")
                .full_addr(originInsOrder.getAddress())
                .email("")
                .mobile(originInsOrder.getPhone())
                .build();
        CommonLable applicant = CommonLable.builder()
                .prodCode(insuranceConfigProperties.getProd_code())
                .relTbl("ins_appl_applnt")
                .relFieldName(applicantFieldName).build();
        //构造被保人信息
        InsuredFieldName insuredFieldName = InsuredFieldName.builder()
                .insrnt_name(originInsOrder.getName())
                .certf_no(originInsOrder.getIdcard())
                .certf_type("01")
                .clnt_type("1")
                .email("")
                .full_addr(originInsOrder.getAddress())
                .mobile(originInsOrder.getPhone())
                .build();
        CommonLable insuranced = CommonLable.builder()
                .prodCode(insuranceConfigProperties.getProd_code())
                .relTbl("ins_appl_insrnt")
                .relFieldName(insuredFieldName).build();
        //货物标的信息节点
        TgtItemcargoFieldName tgtItemcargoFieldName = TgtItemcargoFieldName.builder()
                .item_description(originInsOrder.getHwmc())
                .build();
        CommonLable tgtItemcargo = CommonLable.builder()
                .prodCode(insuranceConfigProperties.getProd_code())
                .relTbl("ins_plcy_tgt_adress")
                .relFieldName(tgtItemcargoFieldName).build();
        //货运险保单信息节点
        TgtMaincargoFieldName tgtMaincargoFieldName = TgtMaincargoFieldName.builder()
                .start_site_name(originInsOrder.getBegin())
                .end_site_name(originInsOrder.getEndmdd())
                .via_site_name(originInsOrder.getEndmdd())
                .blname(originInsOrder.getCp())
                .conveyance("03")
                .lading_no(originInsOrder.getId() + "")
                .build();
        CommonLable tgtMaincargo = CommonLable.builder()
                .prodCode(insuranceConfigProperties.getProd_code())
                .relTbl("ins_appl_tgt_prop")
                .relFieldName(tgtMaincargoFieldName).build();
        //构造CVRG
        CvrgFieldName cvrgFieldNameOne = CvrgFieldName.builder()
                .amt((originInsOrder.getJe() * 10000) + "")
                .cvrg_code("001")
                .cvrg_name("国内公路货物运输保险条款")
                .is_main_mark("1")
                .plcy_bgn_time(InsUtils.formatYdate(originInsOrder.getTt()))
                .plcy_end_time(InsUtils.oneMonthLater(originInsOrder.getTt()))
                .premium(String.valueOf(originInsOrder.getBxf())).build();
        CommonLable cvrgOne = CommonLable.builder()
                .prodCode(insuranceConfigProperties.getProd_code())
                .relTbl("ins_appl_clause")
                .relFieldName(cvrgFieldNameOne).build();

        CvrgFieldName cvrgFieldNameTwo = CvrgFieldName.builder()
                .amt("0.00")
                .cvrg_code("101")
                .cvrg_name("附加盗窃、抢劫保险条款")
                .is_main_mark("1")
                .plcy_bgn_time(InsUtils.formatYdate(originInsOrder.getTt()))
                .plcy_end_time(InsUtils.oneMonthLater(originInsOrder.getTt()))
                .premium("0.00").build();
        CommonLable cvrgTwo = CommonLable.builder()
                .prodCode(insuranceConfigProperties.getProd_code())
                .relTbl("ins_appl_clause")
                .relFieldName(cvrgFieldNameTwo).build();
        List<CommonLable> cvrgList = new ArrayList<>();
        cvrgList.add(cvrgOne);
        cvrgList.add(cvrgTwo);


        //构造body
        UnderRequestBody underRequestBody = UnderRequestBody.builder()
                .appnum("1")
                .good_code(insuranceConfigProperties.getGood_code())
                .good_name("国内公路运输货物保险")
                .operCode("S0200783")
                .operType("A")
                .permium(String.valueOf(originInsOrder.getBxf()))
                .plan_code(insuranceConfigProperties.getPlanCode())
                .plan_name("国内公路运输货物保险")
                .order_source("0")
                .base(base)
                .applicant(applicant)
                .insured(insuranced)
                .tgtItemcargo(tgtItemcargo)
                .tgtMaincargo(tgtMaincargo)
                .cvrg(cvrgList)
                .build();

        Message message = Message.builder()
                .requestPackage(RequestPackage.builder()
                        .HEAD(requestHead)
                        .BODY(underRequestBody)
                        .build())
                .build();
        return message;
    }


    public Message convertResToChengBaoJsonMessage(OriginInsOrder originInsOrder, MessageResponse messageResponse) {
        SimpleDateFormat rs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        RequestHead requestHead = RequestHead.builder()
                .BUSINESS_UUID(originInsOrder.getUuid())
                .CHANNEL_CODE(insuranceConfigProperties.getCHANNEL_CODE())
                .CLIENT_ENCODE("utf-8")
                .INTERFACE_CODE(insuranceConfigProperties.getUNDER_WRITINT_INTERFACE_CODE_2())
                .INTERFACE_PWD(insuranceConfigProperties.getINTERFACE_PWD())
                .INTERFACE_USER_CODE(insuranceConfigProperties.getINTERFACE_USER_CODE())
                .REQUEST_TIME(rs.format(new Date()))
                .SERVE_CODE(insuranceConfigProperties.getSERVE_CODE())
                .XML_LIST_SUFFIX("").build();

        ChengBaoRequestBody chengBaoRequestBody = ChengBaoRequestBody.builder().orderNo(messageResponse.getResponse_body().getData().getOrderNo())
                .platform("pcweb").build();
        Message message = Message.builder().requestPackage(
                RequestPackage.builder().HEAD(requestHead).BODY(chengBaoRequestBody).build()
        ).build();
        return message;
    }
}
